const router = require('koa-router')();

// 获取考试安排列表
router.post('/api/getExamList', async (ctx, next) => {
  const {
    body: { keyword},
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const list = await global._db.model.exam.getExamList(keyword,studentID);
    ctx.body = {
      code: 0,
      message: 'success',
      data: list,
    };
  } catch (error) {
    global._logger.error('getExamList error::', error);
    ctx.body = {
      code: -1,
      message: '获取考试安排列表失败',
    };
  }
});

// 添加考试安排列表
router.post('/api/addExam', async (ctx, next) => {
  const {
    body: { exam},
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const list = await global._db.model.exam.addExam(exam,studentID);
    if (list.code === -1) {
      ctx.body = {
        code: list.code,
        message: list.message,
      };
    } else {
      ctx.body = {
        code: 0,
        message: 'success',
      };
    }
  } catch (error) {
    global._logger.error('addExam error::', error);
    ctx.body = {
      code: -1,
      message: '新增考试安排失败',
    };
  }
});

// 删除考试安排列表
router.post('/api/removeExam', async (ctx, next) => {
  const {
    body: { id},
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const list = await global._db.model.exam.removeExam(id);
    ctx.body = {
      code: 0,
      message: 'success',
    };
  } catch (error) {
    global._logger.error('removeExam error::', error);
    ctx.body = {
      code: -1,
      message: '删除考试安排失败',
    };
  }
});

// 查看我的成绩
router.post('/api/getMyAchievement', async (ctx, next) => {
  const {
    body: { keyword},
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const list = await global._db.model.exam.getMyAchievement(keyword,studentID);
    ctx.body = {
      code: 0,
      message: 'success',
      data: list,
    };
  } catch (error) {
    global._logger.error('getMyAchievement error::', error);
    ctx.body = {
      code: -1,
      message: '查看我的成绩失败',
    };
  }
});

module.exports = router;